package cn.com.self.apis.portal.controller;

import cn.com.self.apis.portal.entity.MassageEntity;
import cn.com.self.apis.portal.service.intf.IMassageService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Map;

/**
 * @Description
 */
@Controller
@RequestMapping("/massage")
public class MassageController {

    @Autowired
    private IMassageService massageService;


    /**新增留言
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity create(@RequestBody MassageEntity entity, HttpServletRequest request){
        entity.setCreatedata(new Timestamp(System.currentTimeMillis()));
        MassageEntity massageEntity = this.massageService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(massageEntity)));
    }


    /**分页获取留言
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<MassageEntity> pageData = this.massageService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadUser(@PathVariable("id") String id,HttpServletRequest request) {
        MassageEntity sysUserEntity = this.massageService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysUserEntity)));

    }


    /**修改留言
     * @Description //TODO
     * @return
     **/

    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody MassageEntity entity, HttpServletRequest request) {
        MassageEntity massageEntity = this.massageService.load(id);
        CopyUtils.copyProperties(entity, massageEntity);
        MassageEntity info = this.massageService.update(massageEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));

    }


    /**删除留言
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
        this.massageService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
}
